package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * ClassName: OrderController
 * Package:
 * Description
 * 订单管理接口
 *
 * @Author xw
 * @Create 2023/11/26 11:11
 */

@RestController
@Api(tags = "订单管理接口")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {

    // 订单
    @Autowired
    private OrderService orderService;


    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result putCancel(OrdersPageQueryDTO ordersPageQueryDTO) {

        log.info("管理端_订单搜索：{}", ordersPageQueryDTO);

        PageResult pageResult = orderService.PutCancel(ordersPageQueryDTO);

        return Result.success(pageResult);

    }


    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");

        OrderStatisticsVO orderStatisticsVO = orderService.Statistics();

        return Result.success(orderStatisticsVO);
    }


    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result detailsId(@PathVariable String id) {
        log.info("查询订单详情：{}", id);
        Orders orders = orderService.orderDetailID(Integer.valueOf(id));

        return Result.success(orders);
    }

    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("接单：{}", ordersRejectionDTO);

        Orders orders = new Orders();
        orders.setId(ordersRejectionDTO.getId()); // 那个订单
        orders.setStatus(3);  // 接单

        orderService.update(orders);
        return Result.success();
    }


    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒单：{}", ordersRejectionDTO);

        Orders orders = new Orders();
        orders.setId(ordersRejectionDTO.getId()); // 那个订单

        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());  // 拒单原因

        orders.setStatus(7);  // 拒单
        orders.setPayStatus(2);

        orderService.update(orders);

        return Result.success();
    }


    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单：{}", ordersCancelDTO);

        Orders orders = new Orders();
        orders.setId(ordersCancelDTO.getId()); // 那个订单
        orders.setRejectionReason(ordersCancelDTO.getCancelReason());  // 取消原因
        orders.setStatus(6);  // 拒单
        orders.setPayStatus(2);

        orderService.update(orders);

        return Result.success();
    }

    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result deliveryId(@PathVariable String id) {
        log.info("派送订单：{}", id);

        Orders orders = new Orders();
        orders.setId(Long.valueOf(id));
        orders.setStatus(4);
        orderService.update(orders);

        return Result.success();
    }


    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result completeId(@PathVariable String id) {
        log.info("完成订单：{}", id);

        Orders orders = new Orders();
        orders.setId(Long.valueOf(id));
        orders.setStatus(5);
        orders.setDeliveryTime(LocalDateTime.now());

        orderService.update(orders);

        return Result.success();
    }

}
